package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.ExperimentGroupMapper;
import com.brillilab.domain.po.experiments.Experiment;
import com.brillilab.domain.po.experiments.ExperimentGroup;
import com.brillilab.domain.po.kit.KitUsage;
import com.brillilab.domain.vo.experiments.ExperimentGroupListVo;
import com.brillilab.service.core.experiments.IExperimentGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 实验分组 服务实现类
 * </p>
 *
 * @author WuMenghao
 * @since 2019-09-05
 */
@Service
@Transactional
public class ExperimentGroupServiceImpl implements IExperimentGroupService {
    @Resource
    private ExperimentGroupMapper experimentGroupMapper;

    @Override
    public ExperimentGroup selectById(Long id) {
        Assert.isTrue(id != null, "请求参数缺失!");
        return experimentGroupMapper.selectById(id);
    }

    @Override
    public List<ExperimentGroup> selectList(Long experimentId) {
        Assert.isTrue(experimentId != null, "请求参数缺失!");
        LambdaQueryWrapper<ExperimentGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExperimentGroup::getExperimentId, experimentId);
        wrapper.orderByAsc(ExperimentGroup::getSort);
        return experimentGroupMapper.selectList(wrapper);
    }

    @Override
    public ExperimentGroup insert(ExperimentGroup entity) {
        Assert.isTrue(entity != null, "请求参数缺失!");
        int insert = experimentGroupMapper.insert(entity);
        Assert.isTrue(insert == 1, "操作失败！");
        return entity;
    }

    @Override
    public boolean updateById(ExperimentGroup entity) {
        Assert.isTrue(entity != null, "请求参数缺失!");
        return experimentGroupMapper.updateById(entity) == 1;
    }

    @Override
    public boolean deleteById(Long id) {
        Assert.isTrue(id != null, "请求参数缺失!");
        return experimentGroupMapper.deleteById(id) == 1;
    }

    @Override
    public boolean deleteByBatch(List<Long> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            return experimentGroupMapper.deleteByBatch(ids) == ids.size();
        }
        return true;
    }

    @Override
    public boolean updateByBatch(List<ExperimentGroup> updateList) {
        if (!CollectionUtils.isEmpty(updateList)) {
            return experimentGroupMapper.updateByBatch(updateList) == updateList.size();
        }
        return true;
    }

    @Override
    public boolean insertByBatch(List<ExperimentGroup> insertList) {
        if (!CollectionUtils.isEmpty(insertList)) {
            return experimentGroupMapper.insertByBatch(insertList) == insertList.size();
        }
        return true;
    }

    @Override
    public List<ExperimentGroupListVo> selectByTopicTargetId(Long topicTargetId) {
        Assert.isTrue(topicTargetId != null, "请求参数缺失!");
        return experimentGroupMapper.selectByTopicTargetId(topicTargetId);
    }

    @Override
    public boolean isExistByExperimentId(Long experimentId) {
        LambdaQueryWrapper<ExperimentGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExperimentGroup::getExperimentId, experimentId);
        return experimentGroupMapper.selectCount(wrapper) > 0;
    }

}
